﻿using DAO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication.Models
{
    public class ServerObjectModel
    {
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        [AllowHtml]
        public string CustomizationXml { get; set; }

        
        public string BuildParameters { get; set; }

        [Required]
        public string DeployParameters { get; set; }

        [Required]
        public string SrcUrl { get; set; }

        [Required]
        public string SvnUserName { get; set; }

        [Required]
        public string SvnUserPassword { get; set; }

        [Required]
        public long SvnRevision { get; set; }

        public string MainCsProjFile { get; set; }

        public int IdType { get; set; }

        public int IdCustomization { get; set; }

        public int IdServer { get; set; }

        public int IdBuildParameter { get; set; }

        public int IdDeployParameter { get; set; }

        public int IdSvnParameter { get; set; }

        public string Version { get; set; }

        public string Notifications { get; set; }

        //public string[] allNotifications { get; set; }

        public string[] appNotifications { get; set; }

        public DeployObject ToEfEntity()
        {
            DataManager dm = new DataManager();
            if (Id != 0)
            {
                Customization customization = new Customization
                {
                    id = IdCustomization,
                    CustomizationXml = CustomizationXml.Replace("&lt;", "<").Replace("&gt;", ">")
                };
                SvnParameter svn = new SvnParameter
                {
                    id = IdSvnParameter,
                    SrcUrl = SrcUrl,
                    SvnRevision = SvnRevision,
                    SvnUserName = SvnUserName,
                    SvnUserPassword = SvnUserPassword
                };
                BuildParameter buildParameter = null;
                if (!string.IsNullOrEmpty(BuildParameters))
                {
                    buildParameter = new BuildParameter
                    {
                        id = IdBuildParameter,
                        Name = "System_" + Name + "_BuildParameters",
                        BuildCommand = BuildParameters
                    };
                }
                DeployParameter deployParameter = null;
                if (!string.IsNullOrEmpty(DeployParameters))
                {
                    deployParameter = new DeployParameter
                    {
                        id = IdDeployParameter,
                        Name = "System_" + DeployParameters + "_BuildParameters",
                        DeployParameters = DeployParameters
                    };
                }
                List<NotificationsApplication> notis = new List<NotificationsApplication>();
                if (appNotifications != null)
                {
                    foreach (var item in appNotifications)
                    {
                        notis.Add(
                                new NotificationsApplication
                                {
                                    IdNotification = Int32.Parse(item),
                                    IdDeployObject = Id
                                    //Notification = dm.NotificationsManager.GetById(Int32.Parse(item));
                                }
                            );
                    }
                }
                else
                    appNotifications = null;
                return new DeployObject
                {
                    Name = Name,
                    Customization = customization,
                    DeployParameter = deployParameter,
                    SvnParameter = svn,
                    BuildParameter = buildParameter,
                    IsDelete = 0,
                    id = Id,
                    IdType = 1, //экземпляр приложения
                    IdBuildParameter = IdBuildParameter,
                    IdCustomization = IdCustomization,
                    IdDeployParameter = IdDeployParameter,
                    IdSvnParameters = IdSvnParameter,
                    IdServer = IdServer,
                    MainCsProjFileName = MainCsProjFile,
                    NotificationsApplications = notis
                };
            }
            Customization customizationNew = new Customization
            {
                CustomizationXml = CustomizationXml.Replace("&lt;", "<").Replace("&gt;", ">")
            };
            SvnParameter svnNew = new SvnParameter
            {
                SrcUrl = SrcUrl,
                SvnRevision = SvnRevision,
                SvnUserName = SvnUserName,
                SvnUserPassword = SvnUserPassword
            };
            BuildParameter buildParameterNew = null;
            if (!string.IsNullOrEmpty(BuildParameters))
            {
                buildParameterNew = new BuildParameter
                {
                    Name = "System_" + Name + "_BuildParameters",
                    BuildCommand = BuildParameters
                };
            }
            DeployParameter deployParameterNew = null;
            if (!string.IsNullOrEmpty(DeployParameters))
            {
                deployParameterNew = new DeployParameter
                {
                    Name = "System_" + DeployParameters + "_BuildParameters",
                    DeployParameters = DeployParameters
                };
            }
            List<NotificationsApplication> notifications = new List<NotificationsApplication>();

            if (!string.IsNullOrEmpty(Notifications))
            {
                string[] nots = Notifications.Split(';');
                foreach (var s in nots)
                {
                    int id;
                    if (Int32.TryParse(s, out id))
                        notifications.Add(new NotificationsApplication
                        {
                            IdDeployObject = Id,
                            IdNotification = id
                        });
                }
            }

            return new DeployObject
            {
                Name = Name,
                Customization = customizationNew,
                DeployParameter = deployParameterNew,
                SvnParameter = svnNew,
                BuildParameter = buildParameterNew,
                IsDelete = 0,
                id = Id,
                NotificationsApplications = notifications,
                IdType = 1 //экземпляр приложения\
            };
        }

        public static ServerObjectModel ToServerObjectModel(DeployObject deployObject)
        {
            return new ServerObjectModel
            {
                BuildParameters = deployObject.BuildParameter.BuildCommand,
                CustomizationXml = deployObject.Customization.CustomizationXml,
                DeployParameters = deployObject.DeployParameter.DeployParameters,
                Name = deployObject.Name,
                SrcUrl = deployObject.SvnParameter.SrcUrl,
                SvnRevision = deployObject.SvnParameter.SvnRevision == null ? 0 : (long)deployObject.SvnParameter.SvnRevision,
                SvnUserName = deployObject.SvnParameter.SvnUserName,
                SvnUserPassword = deployObject.SvnParameter.SvnUserPassword,
                MainCsProjFile = deployObject.MainCsProjFileName,
                Id = deployObject.id,
                IdBuildParameter = (int)deployObject.IdBuildParameter,
                IdDeployParameter = (int)deployObject.IdDeployParameter,
                IdSvnParameter = (int)deployObject.IdSvnParameters,
                IdType = (int)deployObject.IdType,
                IdCustomization = (int)deployObject.IdCustomization,
                IdServer = (int)deployObject.IdServer,
                Version = deployObject.AppVersion
            };
        }
    }
}